package com.example.huanhang.changzhouapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.huanhang.changzhouapp.base.BaseActivity;
import com.example.huanhang.changzhouapp.bean.User;
import com.example.huanhang.changzhouapp.fragment.FileFragment;
import com.example.huanhang.changzhouapp.fragment.HomeFragment;
import com.example.huanhang.changzhouapp.fragment.MineFragment;
import com.example.huanhang.changzhouapp.fragment.NoticeFragment;
import com.example.huanhang.changzhouapp.view.CustomViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    @BindView(R.id.viewPager)
    CustomViewPager viewPager;
    @BindView(R.id.ll_home)
    LinearLayout ll_home;
    @BindView(R.id.ll_gonggao)
    LinearLayout ll_gonggao;
    @BindView(R.id.ll_wenjian)
    LinearLayout ll_file;
    @BindView(R.id.ll_mine)
    LinearLayout ll_mine;
    private ArrayList<Fragment> mFragments;
    private long firstTime = 0;
    private User mUser;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        mFragments = new ArrayList<>();
        viewPager.setScroll(true);
        HomeFragment homeOneFragment = new HomeFragment();
        NoticeFragment noticeFragment = new NoticeFragment();
        FileFragment fragment = new FileFragment();
        MineFragment mineOneFragment = new MineFragment();
        mFragments.add(homeOneFragment);
        mFragments.add(noticeFragment);
        mFragments.add(fragment);
        mFragments.add(mineOneFragment);
    }

    @Override
    public void initData() {
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(4);
        ll_home.setSelected(true);
        setListener();
    }
    protected void setListener() {
        ll_home.setOnClickListener(this);
        ll_gonggao.setOnClickListener(this);
        ll_file.setOnClickListener(this);
        ll_mine.setOnClickListener(this);
        viewPager.addOnPageChangeListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                viewPager.setCurrentItem(0);
                tabSelected(ll_home);
                break;
            case R.id.ll_gonggao:
                viewPager.setCurrentItem(1);
                tabSelected(ll_gonggao);
                break;
            case R.id.ll_wenjian:
                viewPager.setCurrentItem(2);
                tabSelected(ll_file);
                break;
            case R.id.ll_mine:
                viewPager.setCurrentItem(3);
                tabSelected(ll_mine);
                break;
        }
    }

    private void tabSelected(LinearLayout linearLayout) {
        ll_home.setSelected(false);
        ll_gonggao.setSelected(false);
        ll_file.setSelected(false);
        ll_mine.setSelected(false);
        linearLayout.setSelected(true);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                tabSelected(ll_home);
                break;
            case 1:
                tabSelected(ll_gonggao);
                break;
            case 2:
                tabSelected(ll_file);
                break;
            case 3:
                tabSelected(ll_mine);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    protected boolean enableSliding() {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 1800) {
                moveTaskToBack(false);
            } else {
                Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(false);
        super.onBackPressed();
    }


    private class MyAdapter extends FragmentPagerAdapter {
        MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
